/**
 * 
 */
package com.pengtu.web.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.SuperviseGroup;
import com.pengtu.entity.app.SupervisorGroup;
import com.pengtu.entity.app.User;
import com.pengtu.service.app.SuperviseManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * @author Administrator
 *
 */
public class SupervisorAction extends BaseActionSupport<SupervisorGroup> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SuperviseManager superviseManager;
	
	
	private String id ;
	private SupervisorGroup supervisorGroup;
	private List<SupervisorGroup> supervisorList;
	private Page<SupervisorGroup> page = new Page<SupervisorGroup>(10);
	private SuperviseGroup superviseGroup; 
	private  User officer;
	
	@Override
	public SupervisorGroup getModel() {
		// TODO Auto-generated method stub
		return supervisorGroup;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		if(!page.isOrderBySetted()){
			page.order(Page.ASC);
			page.orderBy("createDate");
		}
		page = superviseManager.findPageSor(page, queryFilter);
		for (SupervisorGroup sor : page.getResult()) {
			System.out.println(sor.getSuperviseGroup().getId());
		}
		return SUCCESS;
	}
	
	
	/**
	 * 根据部门ID查询内部人员
	 * 
	 * @return
	 * @throws Exception
	 */
	public String searchChooseBySeId() throws Exception {
		String sperviseId = Struts2Utils.getParameter("sperviseId");
		List<User> users = new ArrayList<User>();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<SupervisorGroup> susors = superviseManager.findBySorPropertyList("superviseGroup.id", sperviseId);
		if(susors.size()>0){
			for (SupervisorGroup susor :susors) {
				users.add(susor.getOfficer());
			}
			for (User user : users) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", user.getId());
				map.put("userName", user.getUserName());
				map.put("name", user.getName());
				map.put("mobilePhone", user.getMobilePhone());
				map.put("email", user.getEmail());
				list.add(map);
			}
		}
		Struts2Utils.renderJsontoExtTotalResults(list);
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		supervisorGroup.setOfficer(officer);
		supervisorGroup.setSuperviseGroup(superviseGroup);
		superviseManager.saveSor(supervisorGroup);
		String msg = "新增";
		if(StringUtils.isNotEmpty(id))
			msg = "修改";
		addActionMessage(msg + "监督人员信息");
		return "successMessage";
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		superviseManager.deleteSor(id);
		Struts2Utils.renderText("1");
		return NONE;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(id)){
			supervisorGroup = superviseManager.findBySorProperty("id", id);
		}else{
			supervisorGroup = new SupervisorGroup();
		}
		
	}
	
	@Autowired
	public void setSuperviseManager(SuperviseManager superviseManager) {
		this.superviseManager = superviseManager;
	}

	public Page<SupervisorGroup> getPage() {
		return page;
	}

	public void setPage(Page<SupervisorGroup> page) {
		this.page = page;
	}

	public List<SupervisorGroup> getSupervisorList() {
		return supervisorList;
	}


	public void setId(String id) {
		this.id = id;
	}

	public void setOfficer(User officer) {
		this.officer = officer;
	}

	public void setSuperviseGroup(SuperviseGroup superviseGroup) {
		this.superviseGroup = superviseGroup;
	}
	
	
	
	
	

}
